﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.BOM;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.BOMCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.BOMCommandHandler
{
    /// <summary>
    /// 添加物料信息
    /// 物联网-智能MES生产管理系统-01-BOM管理V1.1-20250206
    /// </summary>
    public class AddBomPartsCommandHandler : IRequestHandler<AddBomPartsCommand, APIResult<int>>
    {
        private readonly IBaseRepository<BomParts> partRep;
        private readonly IMapper mapper;

        public AddBomPartsCommandHandler(IBaseRepository<BomParts> partRep, IMapper mapper)
        {
            this.partRep = partRep;
            this.mapper = mapper;
        }
        public Task<APIResult<int>> Handle(AddBomPartsCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            var model=mapper.Map<BomParts>(request);
            var res=partRep.Add(model).Result;
            if (res > 0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
                result.data = res;
            } 
            return Task.FromResult(result);
        }
    }
}
